using System;
using System.Web;
using System.Xml;
namespace HKB.Common
{
	public class Language
	{
		public static string Get(string key)
		{
			string str = AppSetings.Get("AppPrefix");
			XmlDocument xmlDocument = new XmlDocument();
			string text = str + "LanguageDoc";
			if (HttpContext.Current.Cache[text] == null)
			{
				string installDir = AppSetings.Get("InstallDir");
				string path = Language.GetPath(installDir);
				xmlDocument = new XmlDocument();
				try
				{
					xmlDocument.Load(path);
				}
				catch (System.Exception ex)
				{
					HttpContext.Current.Response.Write("语言包数据结构错误！<br/>the Wrong of Langage Data Pack!<br/>可能原因：" + ex.Message.ToString());
					HttpContext.Current.Response.End();
				}
				DataCache.SetCacheDepFile(text, xmlDocument, path);
			}
			else
			{
				xmlDocument = (XmlDocument)HttpContext.Current.Cache[text];
			}
			XmlElement documentElement = xmlDocument.DocumentElement;
			XmlNode xmlNode = documentElement.SelectSingleNode(key);
			string result;
			if (xmlNode == null)
			{
				result = "";
			}
			else
			{
				if (xmlNode.InnerText != null)
				{
					string innerText = xmlNode.InnerText;
					result = innerText;
				}
				else
				{
					result = "";
				}
			}
			return result;
		}
		public static string Get(string key, string[] myparam)
		{
			string text = Language.Get(key);
			for (int i = 0; i < myparam.Length; i++)
			{
				text = text.Replace("{" + i + "}", myparam[i]);
			}
			return text;
		}
		public static string Get(string key, string myparam)
		{
			string text = Language.Get(key);
			return text.Replace("{0}", myparam);
		}
		public static string GetAdminMsg(string key)
		{
			return Language.Get("Admin/" + key);
		}
		public static string GetAdminMsg(string key, string[] myparam)
		{
			return Language.Get("Admin/" + key, myparam);
		}
		public static string GetAdminMsg(string key, string myparam)
		{
			return Language.Get("Admin/" + key, myparam);
		}
		public static string GetMemberMmbsg(string key)
		{
			return Language.Get("Member/" + key);
		}
		public static string GetMemberMsg(string key, params string[] myparam)
		{
			return Language.Get("Member/" + key, myparam);
		}
		public static string GetPath(string installDir)
		{
			string text = Fetch.GetCookies("language");
			if (string.IsNullOrEmpty(text))
			{
				text = "cn";
			}
			string truePath = Fetch.GetTruePath(installDir + "language/" + text + ".xml");
			FileIO fileIO = new FileIO();
			string result;
			if (fileIO.FileExists(truePath))
			{
				result = truePath;
			}
			else
			{
				result = Fetch.GetTruePath(installDir + "language/cn.xml");
			}
			return result;
		}
	}
}
